#ifndef KEY_DEVICE_H
#define KEY_DEVICE_H

#include <QObject>
#include <QSerialPort>
#include <QTimer>

class KeyDevice : public QObject
{
    Q_OBJECT
  public:
    enum KeyCode : unsigned char
    {
        Key_A = 0x05,
        Key_B = 0x06,
        Key_C = 0x07,
        Key_D = 0x08,
        Key_Right2 = 0x09,
        Ket_Right1 = 0x0A,
        Key_Left2 = 0x0B,
        Key_Left1 = 0x0C,
        Key_D1 = 0x11,
        Key_D2 = 0x12,
        Key_D3 = 0x13,
        Key_D4 = 0x14,
        Key_D5 = 0x15,
        Key_Stop = 0x16,
    };

  private:
    // QTimer *timer;
    QSerialPort *serialPort;
    QByteArray buffer;

  public:
    explicit KeyDevice(QObject *parent = nullptr);
    ~KeyDevice();
    bool InitDevice(const QString &device, int rate);

  private:
    bool CheckFrame(const QByteArray &frame);
    void DecodeFrame(const QByteArray &frame);
    void ReadData();

  signals:
    void OnInput(KeyCode key);
};

#endif // KEY_DEVICE_H
